package com.shawn.ui;

import android.content.Context;
import android.util.AttributeSet;
import android.view.View;
import android.widget.CheckBox;
import android.widget.RelativeLayout;
import android.widget.TextView;

import com.shawn.mobliesafe.R;

/**
 * Created by shawn on 2016/4/6.
 */
public class SettingView extends RelativeLayout {

    private TextView tv_setting_title;
    private TextView tv_setting_des;
    private CheckBox cb_setting_update;

    private String des_on;
    private String des_off;

    public SettingView(Context context) {
        super(context);
        init();
    }

    public SettingView(Context context, AttributeSet attrs) {
        super(context, attrs);
        init();

        //通过命名空间和属性的名称获取属性的值
        //namespace : 命名空间
        //name : 属性名称
        String mtitle = attrs.getAttributeValue("http://schemas.android.com/apk/res-auto","mtitle");
        des_on = attrs.getAttributeValue("http://schemas.android.com/apk/res-auto","des_on");
        des_off = attrs.getAttributeValue("http://schemas.android.com/apk/res-auto","des_off");
        //给自定义组合控件的控件设置相应的值
        //初始化控件的值
        tv_setting_title.setText(mtitle);
        if(isChecked()) {
            tv_setting_des.setText(des_on);
        }else {
            tv_setting_des.setText(des_off);
        }
    }

    public SettingView(Context context, AttributeSet attrs, int defStyleAttr) {
        super(context, attrs, defStyleAttr);
        init();
    }

    private void init() {
        //获取view对象,同时给veiw对象设置父控件,相当于先创建一个view对象,在把控件放到自定义控件中
        View view = View.inflate(getContext(), R.layout.settingview,this);
        tv_setting_title = (TextView)view.findViewById(R.id.tv_setting_title);
        tv_setting_des = (TextView)view.findViewById(R.id.tv_setting_des);
        cb_setting_update = (CheckBox)view.findViewById(R.id.cb_setting_update);
    }

    //需要添加一些方法,使程序员能方便的去改变自定义控件中的控件的值
    /**
     * 设置标题的方法
     */
    public void setTitle(String title){
        tv_setting_title.setText(title);
    }

    /**
     * 设置描述信息的方法
     */
    public void setDes(String des){
        tv_setting_des.setText(des);
    }
    /**
     * 设置checkbox状态
     */
    public void setChecked(boolean isChecked){
        cb_setting_update.setChecked(isChecked);
        if(isChecked()) {
            tv_setting_des.setText(des_on);
        }else {
            tv_setting_des.setText(des_off);
        }
    }

    /**
     * 获取checkbox的状态
     */
    public boolean isChecked(){
        return cb_setting_update.isChecked();
    }
}
